﻿Imports ControllerSigeco

Public Class frmMercaderia

    Dim classController As New controller_stock

    Private _idMercaderia As Integer = 0

    Public Property idMercaderia() As Integer
        Get
            Return _idMercaderia
        End Get
        Set(value As Integer)
            _idMercaderia = value
        End Set
    End Property

    Private Sub frmMercaderia_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        CargarCombos()
        CargarMercaderias()
    End Sub

    Private Sub CargarCombos()
        Dim dsMarca As New DataTable
        dsMarca = classController.get_marcas(oiml.dbConn, 0, "")
        If Not dsMarca Is Nothing Then
            cboMarca.DataSource = dsMarca
            cboMarca.DisplayMember = "marca"
            cboMarca.ValueMember = "idMarca"
        End If

        Dim dsRubro As New DataTable
        dsRubro = classController.get_rubros(oiml.dbConn, 0, "")
        If Not dsRubro Is Nothing Then
            cboRubro.DataSource = dsRubro
            cboRubro.DisplayMember = "rubro"
            cboRubro.ValueMember = "idRubro"
        End If

        Dim dsImpuesto As New DataTable
        dsImpuesto = classController.get_impuestos(oiml.dbConn, 0, "")
        If Not dsImpuesto Is Nothing Then
            cboImpuesto.DataSource = dsImpuesto
            cboImpuesto.DisplayMember = "impuesto"
            cboImpuesto.ValueMember = "idImpuesto"
        End If

    End Sub

    Private Sub CargarMercaderias()
        Try
            If idMercaderia > 0 Then

                Dim ds As New DataTable
                ds = classController.get_mercaderias_All(oiml.dbConn, idMercaderia, "", 0, 0)

                If Not ds Is Nothing Then
                    txtMercaderia.Text = ds.Rows(0)("mercaderia").ToString()
                    txtStockMinimo.Text = ds.Rows(0)("stockMinimo").ToString()
                    cboMarca.SelectedValue = ds.Rows(0)("idMercaderia").ToString()
                    cboRubro.SelectedValue = ds.Rows(0)("idRubro").ToString()
                    txtObs.Text = ds.Rows(0)("observaciones").ToString()
                    cboImpuesto.SelectedValue = ds.Rows(0)("idImpuesto").ToString()
                    txtActualizadoPor.Text = ds.Rows(0)("usuario").ToString()
                    txtEnFecha.Text = ds.Rows(0)("actualizadoFecha").ToString()
                    btnEliminar.Enabled = True
                Else

                End If

                txtMercaderia.Focus()

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Function ValidarCampos() As Boolean

        Dim controlCampos As Boolean = False

        If txtMercaderia.Text = "" Then
            ErrorProvider1.SetError(txtMercaderia, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtMercaderia, "")
        End If        '

        If txtStockMinimo.Text = "" Or Not IsNumeric(txtStockMinimo.Text) Then
            ErrorProvider1.SetError(txtStockMinimo, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtStockMinimo, "")
        End If        '

        If Not IsNumeric(cboMarca.SelectedValue) Then
            ErrorProvider1.SetError(cboMarca, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(cboMarca, "")
        End If        '

        If Not IsNumeric(cboRubro.SelectedValue) Then
            ErrorProvider1.SetError(cboRubro, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(cboRubro, "")
        End If        '

        If Not IsNumeric(cboImpuesto.SelectedValue) Then
            ErrorProvider1.SetError(cboImpuesto, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(cboImpuesto, "")
        End If        '

        Return controlCampos

    End Function

    Private Sub btnGrabar_Click(sender As System.Object, e As System.EventArgs) Handles btnGrabar.Click

        If ValidarCampos() Then
            Exit Sub
        End If

        If idMercaderia = 0 Then
            idMercaderia = classController.Insert_mercaderia(oiml.dbConn, txtMercaderia.Text, txtStockMinimo.Text, cboMarca.SelectedValue, cboRubro.SelectedValue, txtObs.Text, cboImpuesto.SelectedValue, oiml.idUsuario)
            If idMercaderia > 0 Then
                MessageBox.Show("Registro grabado con exito!", "Mercaderias", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.Close()

            Else
                MessageBox.Show("Error: " & classController.MessageError.Message, "Mercaderias", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        Else
            If classController.Update_mercaderia(oiml.dbConn, idMercaderia, txtMercaderia.Text, txtStockMinimo.Text, cboMarca.SelectedValue, cboRubro.SelectedValue, txtObs.Text, cboImpuesto.SelectedValue, oiml.idUsuario) Then
                MessageBox.Show("Registro grabado con exito!", "Mercaderias", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Me.Close()

            Else
                MessageBox.Show("Error: " & classController.MessageError.Message, "Mercaderias", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End If

    End Sub

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEliminar_Click(sender As System.Object, e As System.EventArgs) Handles btnEliminar.Click

        If idMercaderia > 0 Then
            If MessageBox.Show("Desea eliminar el registro seleccionado?", "Mercaderias", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                If classController.Delete_mercaderia(oiml.dbConn, idMercaderia) Then
                    Me.Close()

                Else
                    MessageBox.Show("Error: " & classController.MessageError.Message, "Mercaderias", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If

        End If

    End Sub

End Class